/**
 *
 */
package com.naiterui.ehp.bs.doctor.dao.impl;

import com.naiterui.ehp.bp.domain.DrPatientGroupDetail;
import com.naiterui.ehp.bs.doctor.dao.IDrPatientGroupDetailDao;
import com.naiterui.ehp.bs.doctor.repository.IDrPatientGroupDetailRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 医生患者分组详情
 * @author guoyongxiang
 * @since 2.7.0
 */
@Component
public class AdDrPatientGroupDetailDaoImpl  implements IDrPatientGroupDetailDao {

    @Autowired
    private IDrPatientGroupDetailRepository drPatientGroupDetailRepository;

    @Override
    public List<Long> getPatientIdsByGroupId(Long groupId, Long doctorId) {
        List<DrPatientGroupDetail>  list = drPatientGroupDetailRepository.findByGroupIdAndDoctorId(groupId, doctorId);
        return list.stream().map(DrPatientGroupDetail::getPatientId).collect(Collectors.toList());
    }

    @Override
    public List<DrPatientGroupDetail> getDetailByGroupIds(List<Long> groupIds, Long doctorId) {
        return drPatientGroupDetailRepository.findByGroupIdInAndDoctorId(groupIds, doctorId);
    }

    @Override
    public List<DrPatientGroupDetail> getDetailByDoctorId(Long doctorId) {
        return drPatientGroupDetailRepository.findByDoctorId(doctorId);
    }

    @Override
    public List<DrPatientGroupDetail> getDetailByDoctorId(Long doctorId, List<Long> patientIds) {
        List<DrPatientGroupDetail> patientGroupDetails = drPatientGroupDetailRepository.findByDoctorIdAndPatientIdIn(doctorId, patientIds);
        return patientGroupDetails;
    }

    @Override
    public void savePatientsByGroupId(Long groupId, Long doctorId, List<Long> patientIds) {
        if (patientIds != null && patientIds.size() > 0) {
           List<DrPatientGroupDetail> list = patientIds.stream().map(patientId -> {
                DrPatientGroupDetail detail = new DrPatientGroupDetail();
                detail.setGroupId(groupId);
                detail.setDoctorId(doctorId);
                detail.setPatientId(patientId);
                detail.setCreatedAt(new Date());
                detail.setCreatedBy("doctorSystem");
                detail.setChangedAt(new Date());
                detail.setChangedBy("doctorSystem");
                return detail;
            }).collect(Collectors.toList());
            drPatientGroupDetailRepository.save(list);
        }
    }

    @Override
    public void removePatientsByGroupId(Long groupId, List<Long> patientIds) {
        if (patientIds != null && !patientIds.isEmpty()) {
            drPatientGroupDetailRepository.deleteByGroupIdAndPatientIdIn(groupId, patientIds);
        } else {
            drPatientGroupDetailRepository.deleteByGroupId(groupId);
        }
    }

    @Override
    public void savePatientByGroupIds(Long doctorId, Long patientId, Set<Long> groupIds) {
        if (groupIds != null && groupIds.size() > 0) {
            List<DrPatientGroupDetail> list = groupIds.stream().map(groupId -> {
                DrPatientGroupDetail detail = new DrPatientGroupDetail();
                detail.setGroupId(groupId);
                detail.setDoctorId(doctorId);
                detail.setPatientId(patientId);
                detail.setCreatedAt(new Date());
                detail.setCreatedBy("doctorSystem");
                detail.setChangedAt(new Date());
                detail.setChangedBy("doctorSystem");
                return detail;
            }).collect(Collectors.toList());
            drPatientGroupDetailRepository.save(list);
        }
    }

    @Override
    public void removePatientFromGroupIds(Long patientId, Set<Long> groupIds) {
        if (groupIds == null || groupIds.isEmpty()) {
            return;
        }
        drPatientGroupDetailRepository.deleteByGroupIdInAndPatientId(groupIds, patientId);
    }

    @Override
    public Map<Long, Long> getPatientCountInGroup(Long doctorId) {
        List<Object> resultList = drPatientGroupDetailRepository.getPatientCountInGroup(doctorId);
        Map<Long, Long> map = new HashMap<>();
        if (null != resultList) {
            resultList.forEach(obj -> {
                Object[] objArray = (Object[])obj;
                Long groupId = Long.valueOf(String.valueOf(objArray[0]));
                Long patientCount = Long.valueOf(String.valueOf(objArray[1]));
                map.put(groupId, patientCount);
            });
        }
        return map;
    }

    @Override
    public List<Long> getPatientIdsByGroupIds(List<Long> groupIds) {
        List<DrPatientGroupDetail> list = drPatientGroupDetailRepository.findByGroupIdIn(groupIds);
        List<Long> result = new ArrayList<>();
        if (list != null && list.size() > 0) {
            result = list.stream().filter(Objects::nonNull).map(DrPatientGroupDetail::getPatientId).collect(Collectors.toList());
        }
        return result;
    }

    @Override
    public void removeDoctorPatien(Long doctorId, Long patientId) {
        drPatientGroupDetailRepository.deleteByDoctorIdAndPatientId(doctorId, patientId);
    }

}
